$(window).load(function(){	
	// 初始界面获得分享用户电话
	var thisURL =  decodeURIComponent(window.location.href);
	var sharePhone; //分享人手机号

	if(thisURL.split('?').length > 1){
		var parameter = thisURL.split('?')[1];
		var phoneData = parameter.split("&")[0];
		sharePhone = phoneData.split("=")[1];
		var hidePhone = sharePhone.replace(/^(\d{3})\d{4}(\d+)/,"$1****$2");
		if(regs.sharePhoneReg.test(sharePhone)){
			$(".main_title span").html(hidePhone);
		}else{
			$(".main_title span").html("「e车险保」");
		}
	}else {
		$(".main_title span").html("「e车险保」");
	}
	var enterPhoneNum;// 定义用户输入手机号

	// 点击获取验证码，本地验证手机格式，成功下发送请求到后台发送手机验证码，并局部display控制到验证码填写
	$("#getverification").on("click", function(){
		enterPhoneNum = $("#phonenumber").val();
		if(enterPhoneNum == ""){
			warnText($(".black-bg"),"手机号码不允许为空！",1000);
		}else if(regs.phoneReg.test(enterPhoneNum)) {
			getCode(enterPhoneNum);// 后台发送验证码给客户
		}else{
			warnText($(".black-bg"),"请输入11位手机号码！",1000);
		}
	})

	//点击注册，验证验证码是否一致，本地验证密码格式，成功发送
	$("#register").on("click", function(){
		var enterCode = $("#verification").val().trim();// 用户输入的验证码
		var enterPassword = $("#pwd").val();
		if(messageCode){
			if(enterCode == ""){
				warnText($(".black-bg"),"验证码为空！",1000);
				return false;
			}else if(enterCode != messageCode){
				warnText($(".black-bg"),"验证码错误！",1000);
				return false;
			}
		}
		if(enterPassword == ""){
			warnText($(".black-bg"),"密码不允许为空！",1000);
			return false;
		}else if(!regs.passwordReg.test(enterPassword)){
			warnText($(".black-bg"),"请输入6-20位密码！",1000);
			return false;
		}

		var json = {
			mobile : enterPhoneNum,// 用户手机号
			verifycode : enterCode, // 用户验证码
			password : enterPassword,// 用户密码
			owner_mobile : sharePhone// 分享人手机号
		};
		var url = "http://ydche.com/api/uic/userregister.json";
		$.ajax({
			type: "POST",
			url: url,
			async:true,
			data: JSON.stringify(json),
			contentType: "application/json; charset=utf-8",
			dataType: "json",
			timeout : 1000*60*30,
			success: function(data) {
				console.log("返回数据"+JSON.stringify(data));
				if (data.result == 1999) {
					warnText($(".black-bg"),"注册成功！",1000);
					locTimer = setTimeout(function(){
						clearInterval(locTimer);
						window.location.href="http://ydche.com/rest/info/download";
					},1000)
				} else {
					warnText($(".black-bg"),"注册失败:"+data.msg,1000);
				}
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				console.log("............POST..............");
				console.log("state="+XMLHttpRequest.readyState);
				console.log("status="+XMLHttpRequest.status);
				console.log("errorThrown="+errorThrown);
				console.log("............POST..............");
			}
		});
	})

	// 1分钟倒计时及重新获取按钮
	function reacquires(enterPhoneNum){
		$("#regetverification").off("click");
		var seconds = 59;
		var timer = setInterval(function(){
			if(seconds == 0){
				$("#regetverification").html("重新获取");
				clearInterval(timer);
				$("#regetverification").on("click", function(){
					reacquires(enterPhoneNum);
					getCode(enterPhoneNum);
					// 后台发送验证码给客户
				})
			}else{
				$("#regetverification").html("（"+seconds+"s）");
				seconds--;
			}
		},1000);
	}

	var messageCode;// 定义短信验证码

	// 获取短信验证码
	function getCode(enterPhoneNum){
		var url = "http://ydche.com/api/uic/sendcode.json?mobile="+enterPhoneNum+"&type="+1;
		$.ajax({
			type: "GET",
			url: url,
			async: false,
			timeout : 1000*60*30,
			contentType: "application/json; charset=utf-8",
			success: function(data) {
				var result = data;
				console.log(data);
				if (result.result == '1999') {
					warnText($(".black-bg"),"已发送验证码！",1000);
					$(".register_phone").hide();
					$(".register_info").show();
					reacquires(enterPhoneNum);// 开启倒计时
					messageCode = data.code;// 短信验证码赋值
				}else {
					warnText($(".black-bg"),"获取验证码失败!",1000);
				}
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				console.log("............获取验证码..............");
				console.log("state="+XMLHttpRequest.readyState);
				console.log("status="+XMLHttpRequest.status);
				console.log("errorThrown="+errorThrown);
				console.log("............获取验证码..............");
			}
		});
	}
})